﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace LG.Common
{
    [Flags]
    public enum FilterOption
    {
        Telephone = 1,
        Cellphone = 2,
        Email = 4,
        JSString = 8
    }

    public partial class StringHelper
    {
        public static string Filter(string input, FilterOption option)
        {
            return Filter(input, string.Empty, option);
        }
        /// <summary>
        /// 过虑字体串
        /// </summary>
        /// <param name="input">输入的字体串</param>
        /// <param name="replacement">要替代的字体串</param>
        /// <param name="option">过虑选项</param>
        /// <returns></returns>
        public static string Filter(string input, string replacement, FilterOption option)
        {
            if ((option & FilterOption.Cellphone) == FilterOption.Cellphone)
            {
                input = filterCellphone(input, replacement);
            }
            if ((option & FilterOption.Email) == FilterOption.Email)
            {
                input = filterEmail(input, replacement);
            }
            if ((option & FilterOption.Telephone) == FilterOption.Telephone)
            {
                input = filterTelephone(input, replacement);
            }
            if ((option & FilterOption.JSString) == FilterOption.JSString)
            {
                input = filterJSString(input, replacement);
            }
            return input;
        }

        #region 私有方法,每个过滤选项的具体实现

        private static string filterTelephone(string input, string replacement)
        {
            Regex telphone = new Regex(StringCommon.RegTelephoneNoEnds);
            return telphone.Replace(input, replacement);
        }
        private static string filterCellphone(string input, string replacement)
        {
            Regex cellphone = new Regex(StringCommon.RegCellphoneNoEnds, RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline | RegexOptions.IgnoreCase);

            return cellphone.Replace(input, replacement);
        }
        private static string filterEmail(string input, string replacement)
        {
            Regex email = new Regex(StringCommon.RegEmailNoEnds);
            return email.Replace(input, replacement);
        }
        private static string filterJSString(string input, string replacement)
        {
            return Regex.Replace(input, StringCommon.RegJSString, replacement);
        }

        #endregion
    }
    
}
